package com.itheima.goods.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.goods.mapper.SpecMapper;
import com.itheima.goods.pojo.Spec;
import com.itheima.goods.service.SpecService;
import entity.PageResult;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;


@Service
public class SpecServiceImpl extends ServiceImpl<SpecMapper, Spec> implements SpecService {

    @Autowired
    private SpecMapper specMapper;

    @Override
    public List<Spec> findAll() {

        try {
            LambdaQueryWrapper<Spec> queryWrapper = new LambdaQueryWrapper<>();

            List<Spec> specList = specMapper.selectList(queryWrapper);

            return specList;

        } catch (Exception e) {

            e.printStackTrace();

            return null;
        }
    }


    @Override
    public PageResult findPage(Integer page, Integer size) {

        try {
            Page<Spec> specPage = new Page<>(page, size);


            Page<Spec> specPageResult = specMapper.selectPage(specPage, null);

            List<Spec> specList = specPageResult.getRecords();

            long total = specPageResult.getTotal();

            PageResult<Spec> pageResult = new PageResult<>();

            pageResult.setTotal(total);

            pageResult.setRows(specList);

            return pageResult;
        } catch (Exception e) {

            e.printStackTrace();

            return null;
        }
    }


    @Override
    public Spec findById(Integer id) {

        try {
            Spec spec = specMapper.selectById(id);

            return spec;
        } catch (Exception e) {
            e.printStackTrace();

            return null;
        }
    }


    @Override
    public Boolean add(Spec spec) {

//        int i = specMapper.updateById(spec);

        int i = specMapper.insert(spec);


        if (i>0){

            return true;
        }
        return false;
    }

    @Override
    public Boolean update(Integer id, Spec spec) {

        LambdaUpdateWrapper<Spec> updateWrapper = new LambdaUpdateWrapper<>();

        updateWrapper.eq(Spec::getId,id);


        int i = specMapper.update(spec, updateWrapper);

        if (i>0){

            return true;
        }
        return false;
    }

    @Override
    public Boolean delete(Integer id) {

        int i = specMapper.deleteById(id);

        if (i>0){

            return true;
        }
        return false;
    }
}
